package com.ssm.stock.controller;

import com.ssm.dto.CommonResult;
import com.ssm.model.stock.StockModel;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@RestController
public class StockController {

    @GetMapping("/stock/hello")
    public CommonResult<String> hello1() {
        return CommonResult.success("hello");
    }


    @GetMapping("/getStockDetail")
    public CommonResult<StockModel> getStockDetails() {
        StockModel stockModel = new StockModel();
        stockModel.setCode("STOCK==>1000");
        stockModel.setId(1);
        return CommonResult.success(stockModel);
    }


    @SentinelResource("test1")
    @GetMapping("/hello")
    public String hello() {
        return "OK";
    }

    @PostConstruct
    private static void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        FlowRule rule = new FlowRule();
        rule.setResource("test1");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(1);

        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
